/*   
 * Copyright (c) 2011, TUgether, TU Braunschweig
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /**
 * @author Mohamed Karray <mohamed.karray@gmx.de>
 *
 */
 

package de.tu_bs.tugether.liferayunit.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import de.tu_bs.tugether.liferayunit.liferayPages.ILoginPage;
import de.tu_bs.tugether.liferayunit.liferayPages.LiferayPage;
import de.tu_bs.tugether.liferayunit.liferayPages.LoginPage;

public class UserSession  {
	
	public static LiferayPage login(ILoginPage page,String username, String password) throws Exception {
		return page.login(username,password);
	}
	public static LiferayPage login(String login_url, BrowserVersion browserVersion,
			String proxyHost, int port,String username, String password) throws Exception {
		LoginPage page = new LoginPage(new URL(login_url) ,browserVersion,proxyHost, port);
		return page.login(username, password);
	}
	public static LiferayPage login(URL login_url, String username, String password) throws Exception {
		LoginPage page = new LoginPage(login_url );
		return page.login(username, password);
	}
	public static LiferayPage login(String login_url, String username, String password) throws Exception {
		LoginPage page = new LoginPage(new URL(login_url) );
		return page.login(username, password);
	}

	public static LiferayPage loginAsBruno() throws Exception{
		return loginAsBruno("http://localhost:8080");
	}
	
	public static LiferayPage loginAsRichard() throws Exception{
		return loginAsRichard("http://localhost:8080");
	}
	
	public static LiferayPage loginAsBruno(String portalUrl) throws Exception {
		String achnorUserName = "bruno";
		return simpleLogin(portalUrl, achnorUserName);
	}
	private static LiferayPage simpleLogin(String portalUrl, String achnorUserName) throws IOException, MalformedURLException, Exception {
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
		webClient.setThrowExceptionOnScriptError(true);
		HtmlPage page = webClient.getPage(portalUrl);

		HtmlAnchor login_as_bruno = page.getAnchorByText("Login as " + achnorUserName);
		page = login_as_bruno.click();
		return new LiferayPage(page);
	}
	public static LiferayPage loginAsRichard(String portalUrl) throws Exception {
		String achnorUserName = "richard";
		return simpleLogin(portalUrl, achnorUserName);
	}


}
